<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <h3 class="logo"><img src="@/assets/images/login/logo.png" alt="" /></h3>
      <el-form-item prop="phone">
        <!-- <span class="svg-container">
          <svg-icon icon-class="user" />
        </span> -->
        <el-input
          v-model="loginForm.phone"
          name="sh_phone"
          type="text"
          auto-complete="on"
          placeholder="手机号(必填)"
        />
      </el-form-item>
      <el-form-item prop="password">
        <!-- <span class="svg-container">
          <svg-icon icon-class="password" />
        </span> -->
        <el-input
          :type="pwdType"
          v-model="loginForm.password"
          name="sh_password"
          auto-complete="on"
          placeholder="密码(必填)"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="pwdType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item style="border: 0;">
        <el-checkbox :checked="rememberPwd" v-model="rememberPwd"></el-checkbox>
        <span style="color: #999999">&nbsp;记住密码</span>
      </el-form-item>
      <el-button
        :loading="loading"
        type="info"
        plain
        class="login_btn"
        @click.native.prevent="handleLogin"
      >
        登录
      </el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error("密码不能小于5位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        phone:
          (JSON.parse(localStorage.getItem("accountInfo")) &&
            JSON.parse(localStorage.getItem("accountInfo")).phone) ||
          "",
        password: JSON.parse(localStorage.getItem("rememberPwd"))
          ? decodeURIComponent(
              escape(
                atob(
                  JSON.parse(localStorage.getItem("accountInfo")) &&
                    JSON.parse(localStorage.getItem("accountInfo")).password
                )
              )
            )
          : ""
      },
      loginRules: {
        phone: [
          // { required: true, trigger: "blur", validator: validatePhone }
        ],
        password: [{ required: true, trigger: "blur", validator: validatePass }]
      },
      loading: false,
      pwdType: "password",
      redirect: undefined,
      rememberPwd: JSON.parse(localStorage.getItem("rememberPwd")) || false
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              if (this.rememberPwd) {
                let password = btoa(
                  unescape(encodeURIComponent(this.loginForm.password))
                );
                let obj = {
                  phone: this.loginForm.phone,
                  password: password
                };
                localStorage.setItem("rememberPwd", true);
                localStorage.setItem("accountInfo", JSON.stringify(obj));
              } else {
                localStorage.removeItem("rememberPwd");
                localStorage.removeItem("accountInfo");
              }
              this.loading = false;
              this.$router.push({ path: this.redirect || "/" });
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
/* reset element-ui css */
.login-container {
  .el-form-item__content {
    line-height: 22px;
  }
  .el-input {
    display: inline-block;
    height: 44px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 0 0;
      height: 44px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: #606266 !important;
      }
    }
  }
  .el-form-item {
    border-bottom: 1px solid rgba(192, 192, 192, 0.5);
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$dark_gray: #889aa4;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background: url("~@/assets/images/login/bg.png") no-repeat center center;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 456px;
    max-width: 100%;
    padding: 75px 56px 15px 56px;
    margin: 240px auto 0;
    background: #ffffff;
    box-shadow: 0px 2px 12px 0px rgba(87, 142, 253, 0.1);
    border-radius: 18px;
  }
  .logo {
    position: absolute;
    top: -43px;
    left: 50%;
    transform: translateX(-50%);
    margin: 0;
    img {
      display: block;
      width: 100%;
      height: 100%;
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 18px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .login_btn {
    width: 100%;
    height: 48px;
    border-radius: 8px;
    margin: 39px 0 58px 0;
    border: 0;
  }
}
</style>
